/**
 * Created by hcadmin on 16/10/17.
 */
$(function(){
    //获取来自管理日历的参数
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
    })(jQuery);

    var turnDay           = $.getUrlParam('turnDay');
    if(turnDay!=undefined){
        localStorage.turnDay = turnDay;
    }
    //设置权限
    if(localStorage.role!="3"){
        $(".add_model").remove();
        $(".log_nav_right").html("");
    }
    //获取 待我审批 数量
    var url_list = apiurl+"/approvals/list";
    var data = {
        listType   :"0",
        user_id    :localStorage.user_id,
        timestamp  :"0",
        token      :localStorage.token,
        group_id   :localStorage.group_id
    };

    $.ajax({
        url:url_list,
        type:"GET",
        data:data,
        success:function(d){
            if(typeof d == "object"){
                var msg= d.msg;
                if(msg=="202") {
                    $(".token_iframe").fadeIn(1000);
                }
            }else{
                var call_back = JSON.parse(d);
                var code      = call_back.code;
                var data      = call_back.data.list;
                var del_arr   = [];
                if(code=="0"){
                    if(data.length==0){
                        $(".red_dot").hide();
                    }else{
                        for(var i=0;i<data.length;i++){

                            if(data[i].del_flag=="0"){
                                del_arr.push(data[i].del_flag);
                            }
                        }
                        if(del_arr.length==0){
                            $(".red_dot").hide();
                        }else{
                            $(".red_dot").show().html(del_arr.length);
                        }

                    }
                }
            }
        }
    });
    //获取模板
    var url  = apiurl+"/approvals/temp/list";
    var data_model = {
        listType:"1",                            //0-后台
        group_id:localStorage.group_id,          //组id
        approval_status:"1"                      //-1：全部 0：停用的 1：启用的
    };
    $.ajax({
        beforeSend:function(){
            $(".loading").show();
        },
        url:url,
        type:"GET",
        data:data_model,
        success:function(d){
            //var title_length_arr = [];
            //var id_arr = [];
            //var maxIndex = "";
            var call_back = JSON.parse(d);
            var code      = call_back.code;
            var data      = call_back.data;
            if(code=="0"){
                $(".loading").hide();
                for(var i=0;i<data.length;i++){
                    var html = "<div class='col-xs-4 table_big' id='"+data[i].approval_temp_id+"'><div class='table_mid'><img class='table_small_cover' src='"+data[i].approval_icon.replace('10.147.251.212:10106','127.0.0.1:50136')+"'> <div class='table_small_text "+i+"'>"+data[i].approval_title+"</div></div></div>";
                    $(".table_content").prepend(html);
                    //title_length_arr.push(data[i].approval_title.length);
                    //id_arr.push(data[i].approval_temp_id);

                }
                for(var a=1;a<100;){
                    if($(".table_big").length==a){
                        $(".table_content").append("<div class='blank_table_big col-xs-4'></div>");
                    }
                    a+=3;
                }
                //debugger;
                if ($(".table_big").length > 0) {
                    var maxHeight = 0;
                    $(".table_big").each(function(){
                        //debugger;
                        var currentHeight = $(this).height();
                        if(currentHeight > maxHeight){
                            maxHeight = currentHeight;
                        }
                    }).css('height',maxHeight);

                    $(".blank_table_big").css('height',maxHeight);

                }



                //if(title_length_arr.length>0){
                //    var j = title_length_arr[0];
                //    var maxId = id_arr[0];
                //    var maxHeight = title_length_arr[0];
                //    for(key in title_length_arr){
                //        if(title_length_arr[key]>j){
                //            j = title_length_arr[key];
                //            //maxIndex = key;
                //            maxId = id_arr[key];
                //        }
                //    }

                //    $(".table_big").each(function(){
                //        if($(this).attr("id")==maxId){
                //            maxHeight = $(this).css('height');
                //        }
                //    }).css('height',maxHeight);
                //    $('.blank_table_big').css('height',maxHeight);
                //}
            }else{
                $(".call_back_success").html(call_back.msg).show().fadeOut(2000);
            }
        }
    });

    //点击模板
    $(document).on('click','.table_big',function(){
        var approval_temp_id = $(this).attr("id");
        if(approval_temp_id){
            window.location.href = "approvalModel.html?id="+approval_temp_id+"";
        }else {
            window.location.href='approvalAdministration.html'
        }
    });

    //点击返回
    var exit;
    exit = document.createElement('iframe');
    exit.style.display = 'none';
    document.documentElement.appendChild(exit);
    $("#approval_back").on('click',function(){
        if(localStorage.turnDay!=undefined){
            window.location.href = '../../manageCalendars/calendar.html?turnDay='+localStorage.turnDay+'';
            localStorage.removeItem("turnDay");
        }else{
            exit.src = "/exit2home";
        }
    });
});
